package com.okay.window

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import com.okay.testclass.R
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 悬浮窗
 */
class WinActivity : FragmentActivity() {

    var isShow: Boolean = false
    private var winService: Intent? = null
    var text:TextView?= null
    var button4:Button?= null
    private var HomeKeyListener: HomeKeyListener? = null

    //是否在讲课
    var isprelection:Boolean = false


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.win_activity)
        EventBus.getDefault().register(this)//注册，重复注册会导致崩溃
        initHomeKeyListener()
        text = findViewById(R.id.text1)
        button4 =  findViewById(R.id.win_but4)
        //点击弹出弹窗
        findViewById<Button>(R.id.win_but1).setOnClickListener {
            starSuspendWindow()
        }
        //弹窗的显示与隐藏
        findViewById<Button>(R.id.win_but2).setOnClickListener {
            if (SuspendUtils.isServiceRunning(this, TrtcWindowService::class.java.name)) {
                SuspendUtils.checkSuspendedWindowPermission(this) {
                    var bool = !isShow
                    ViewModleSuspend.isVisible.postValue(bool)
                    isShow = bool
                }
            }else{
                Toast.makeText(this, "请开启服务", Toast.LENGTH_SHORT).show()
            }
        }
        //停止与启动服务
        findViewById<Button>(R.id.win_but3).setOnClickListener {
            if (SuspendUtils.isServiceRunning(this, TrtcWindowService::class.java.name)) {
                endSuspendWindow()
            } else {
                starSuspendWindow()

            }
        }
        button4?.setOnClickListener{
            isprelection = true
            text?.text = "开始讲课了"
        }
    }

    //接收消息  接收结束主讲
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: ViewModleSuspend.MessageEvent) {
        when(event.type){
            1->{//结束主讲
                endSuspendWindow()
                isprelection = false
                text?.text = "结束讲课啦"
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)//解绑
        dissHomeKeyListener()
    }

    override fun onResume() {
        super.onResume()
        endSuspendWindow()
    }

    private fun starSuspendWindow(){
        if(!isprelection){//没有讲课的话  就不用启动了
            return
        }

        //先判断有没有启动悬浮窗服务，启动过就不用启动了
        if (SuspendUtils.isServiceRunning(this, TrtcWindowService::class.java.name)){
            return
        }

        winService = Intent(this, TrtcWindowService::class.java)
        startService(winService)
        Toast.makeText(this, "启动服务", Toast.LENGTH_SHORT).show()
        SuspendUtils.checkSuspendedWindowPermission(this) {
            ViewModleSuspend.isShowSuspendWindow.postValue(true)
            isShow = true
        }

    }
    private fun endSuspendWindow(){
        if (SuspendUtils.isServiceRunning(this, TrtcWindowService::class.java.name)) {
            ViewModleSuspend.isVisible.postValue(false)
            ViewModleSuspend.isShowSuspendWindow.postValue(false)//停止服务前记得先把弹窗移除掉
            stopService(winService)
            Toast.makeText(this, "服务停止", Toast.LENGTH_SHORT).show()
        }
    }

    /**
     * 启动对Home键的监听
     */
    private fun initHomeKeyListener(){
        HomeKeyListener = HomeKeyListener(this)
        HomeKeyListener?.setOnHomePressedListener(object : HomeKeyListener.OnHomePressedListener{
            override fun onHomePressed() {
                starSuspendWindow()
            }

            override fun onHomeLongPressed() {
                starSuspendWindow()
            }
        })
        HomeKeyListener?.startWatch()
    }

    /**
     * 关闭对home键的监听
     */
    private fun dissHomeKeyListener(){
        HomeKeyListener?.let {
            it.stopWatch()
        }
    }
}